//
//  logoutModel.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/22.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  退出模型

import UIKit

class logoutModel: NSObject {
    
    //外部闭包变量
    var comfun:((_ _data:String)->Void)?
    
    //实例化
    static let shared = logoutModel()
    
    func logoutSEL(comfun:((_ _data:String)->Void)?) -> Void {
        self.comfun = comfun
        
        if localSave.object(forKey: userToken) as? String != nil {
            let uToken = localSave.object(forKey: userToken) as! String
            
            let param = ["token" : uToken] as [String : String]
            print("\((#file as NSString).lastPathComponent):(\(#line))")
            
            print(param)
            
            
            postWithPath(path: logoutURL, paras: param, success: { (response) in
                //判读返回值是否为空
                guard let dic = response as? NSDictionary else {
                    return
                }
                
                print(dic)
                
                //提取提示语
                let alertmsg = dic["resultmsg"] as! String
                
                
                
                print(dic)
                
                if alertmsg  == "退出成功" {
                    
                    //删除本地token
                    localSave.removeObject(forKey: userToken)
                    localSave.removeObject(forKey: personInfo)
                    localSave.removeObject(forKey: personAddData)
                    localSave.synchronize()
                    
                    //清楚本地数据
                    if let appDomain = Bundle.main.bundleIdentifier {
                        UserDefaults.standard.removePersistentDomain(forName: appDomain)
                    }
                    
//                    MBManager.showBriefAlert(logoutSuccess)
                    
                    _refresh = 0
                    //改变内存中用户名
                    _uName = "单机登陆"
                    
                    //暴漏结果
                    comfun!(alertmsg)
                    
                } else if alertmsg == "该账号已在异地登录，请重新登录" {
                    
                    
                    //删除本地token
                    localSave.removeObject(forKey: userToken)
                    localSave.removeObject(forKey: personInfo)
                    localSave.removeObject(forKey: personAddData)
                    localSave.synchronize()
                    
                    //移除通知函数
                    NotificationCenter.default.removeObserver(self)
                    
                    //清楚本地数据
                    if let appDomain = Bundle.main.bundleIdentifier {
                        UserDefaults.standard.removePersistentDomain(forName: appDomain)
                    }
                    _uName = "点击登陆"
                    
//                    self.navigationController!.popToRootViewController(animated: true)
                    MBManager.showBriefAlert(loginError)
                    
                    
                    //暴漏结果
                    comfun!(alertmsg)
                }
                
            }, failure: { (error) in
                self.comfun!(error.localizedDescription)
                CustomAlertView.shared.alertWithTitle(strTitle: netWrong)
//                print(error.localizedDescription)
            })
            
            
            
        } else {
            MBManager.showBriefAlert("已退出")
            
            self.comfun!("token不存在")
            
//            print("token不存在")
        }
        
    }
}
